<?php

class SegmentosModel extends Segmentos{
	
	public function insertSegmentos($arr){
		try {
			$obj = new Segmentos();
			foreach($arr as $chave => $valor){
				if($chave != 'files'){
					$chave = ucfirst($chave);
					$met = 'set'.$chave;
					$obj->$met($valor);
				}
			}
			$db = new Db;
			$db->Insert('segmentos',$obj);
			$this->lista = $db->res;
		}catch (Exception $e) {
			echo 'Erro: ',  $e->getMessage();
		}
	}
	public function updateSegmentos($arr){
		try {
			$obj = new Segmentos();
			foreach($arr as $chave => $valor){
				if($chave != 'files'){
					$chave = ucfirst($chave);
					$met = 'set'.$chave;
					$obj->$met("'".$valor."'");
				}
			}
			$db = new Db;
			$db->Update('segmentos',$obj);
			$this->lista = $db->res;
		}catch (Exception $e) {
			echo 'Erro: ',  $e->getMessage();
		}
	}
	public function deleteSegmentos($id){
		try {
			$db = new Db;
			$db->Delete('segmentos',$id);
			$this->lista = $db->res;
		} catch (Exception $e) {
			echo 'Erro: ',  $e->getMessage();
		}
	}
	public function consultarSegmentos($parametros){
		try {
			$campos = array('*');
			if($parametros > 0){
				$where = 'idsegmentos = '.$parametros;
			}else{
				$where = '';
			}
			$order = 'idsegmentos LIMIT 1';
			$db = new Db;
			$db->Select('segmentos',$campos,$where,$order);
			$this->lista = $db->res[0];
		}catch (Exception $e) {
			echo 'Erro: ',  $e->getMessage();
		}
	}
	public function listarSegmentos($parametros){
		try {
			$campos = array('*');
			$where = '';
			$order = 'idsegmentos DESC';
			$db = new Db;
			$db->Select('segmentos',$campos,$where,$order);
			$view = new SegmentosView();
			$this->lista = $view->listar($db->res);
		}catch (Exception $e) {
			echo 'Erro: ',  $e->getMessage();
		}
	}
	public function visualizarSegmentos($parametros){
		try {
			$campos = array('*');
			$where = '';
			$order = 'titulo ASC';
			$db = new Db;
			$db->Select('segmentos',$campos,$where,$order);
			$view = new SegmentosView();
			$this->lista = $view->visualizar($db->res);
		}catch (Exception $e) {
			echo 'Erro: ',  $e->getMessage();
		}
	}
	public function dropdownSegmentos(){
		try {
			$campos = array('idsegmentos','nome');
			$where = '';
			$order = 'ordem ASC';
			$db = new Db;
			$db->Select('segmentos',$campos,$where,$order);
			$view = new SegmentosView();
			$this->lista = $view->select($db->res);
		}catch (Exception $e) {
			echo 'Erro: ',  $e->getMessage();
		}
	}
}
?>